package day5

object Test5 {
  def main(args: Array[String]): Unit = {
    val list = List("hello,hello,word","haha,hello,haha,hello,word","haha,word,word,word,word,hello")

    // 统计每个单词出现的次数  最终按照出现次数降序排序输出
    val list2: List[String] = list.flatMap(v => v.split(","))
    val map: Map[String, List[(String,Int)]] = list2.map(v => (v, 1)).groupBy(v => v._1)
    val list3: Map[String, Int] = map.map(v => (v._1, v._2.size))

    val list4: List[(String, Int)] = list3.toList
    val list5: List[(String, Int)] = list4.sortBy(v => v._2).reverse
    list5.foreach(v => {
      println(v._1 + "\t" + v._2)
    })

    println("-------------------------------------------------")


    list.flatMap(v => v.split(","))
                .map(v => (v,1))
                .groupBy(v => v._1)
                .map(v => (v._1,v._2.size))
                .toList.sortBy(v => v._2)
                .reverse
                .foreach(v => {
                println(v._1 + "\t" + v._2)
    })

  }
}
